import axios from "axios";
//引入进度条js
import Nprogress from 'nprogress'
//引入进度条css
import 'nprogress/nprogress.css'
// 引入id header
import {uuId as getId} from './getUserTempId' 
// 引入token
import { default as store } from '@/store';

//小圆圈去除
Nprogress.configure({ showSpinner: false });

//二次封装axios
const request = axios.create({
    baseURL: "/api"
});

//请求拦截器
request.interceptors.request.use(config => {

    //请求拦截器内配置统一请求头 
    config.headers.userTempId = getId()
    
    //判断当前token有没有值
    if(store.state.user.token){
        config.headers.token = store.state.user.token
    }

    //进度条开始
    Nprogress.start()
    return config
})

//响应拦截器
request.interceptors.response.use(
    //成功回调
    response => {
        //进度条结束
        Nprogress.done()
        return response.data
    },
    //失败回调
    (err) => {
        console.log(err)
        //失败终止promise链
        return new Promise(() => { })
    }
)

export default request
